package com.taiyuan.ty.controller;

import com.taiyuan.ty.common.R;
import com.taiyuan.ty.entity.PlaceFavorite;
import com.taiyuan.ty.request.FavoriteRequest;
import com.taiyuan.ty.service.PlaceFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : 周桓宇
 * @description :
 * @createDate : 2025/3/26 下午8:57
 */
@RestController
@RequestMapping("/api/placeFavorite")
public class PlaceFavoriteController {
    @Autowired
    private PlaceFavoriteService placeFavoriteService;

    /**
     * 收藏地点
     * @param favoriteRequest
     * @return
     */
    @PostMapping("/favorite")
    public R<String> favoritePlace(@RequestBody FavoriteRequest favoriteRequest) {
        if (placeFavoriteService.favoritePlace(favoriteRequest.getPlaceId(), favoriteRequest.getUserId())) {
            return R.success("收藏成功");
        } else {
            return R.error("收藏失败");
        }
    }

    /**
     * 取消收藏地点
     * @param favoriteRequest
     * @return
     */
    @PostMapping("/unfavorite")
    public R<String> unfavoritePlace(@RequestBody FavoriteRequest favoriteRequest) {
        if (placeFavoriteService.unfavoritePlace(favoriteRequest.getPlaceId(), favoriteRequest.getUserId())) {
            return R.success("取消收藏成功");
        } else {
            return R.error("取消收藏失败");
        }
    }

    /**
     * 获取用户收藏的地点列表
     * @param userId
     * @return
     */
    @GetMapping("/list")
    public R<List<PlaceFavorite>> getFavoritesByUserId(@RequestParam Long userId) {
        List<PlaceFavorite> favorites = placeFavoriteService.getFavoritesByUserId(userId);
        return R.success(favorites);
    }

    /**
     * 检查是否已收藏
     * @param favoriteRequest
     * @return
     */
    @GetMapping("/check")
    public R<Boolean> isFavorite(@RequestBody FavoriteRequest favoriteRequest) {
        boolean isFavorite = placeFavoriteService.isFavorite(favoriteRequest.getPlaceId(), favoriteRequest.getUserId());
        return R.success(isFavorite);
    }
}
